/*
 * @Author: xiaosihan 389652405@qq.com
 * @Date: 2025-11-10 02:26:40
 * @LastEditors: AI Assistant
 * @LastEditTime: 2024-01-01 00:00:00
 * @FilePath: \taro_vite_react_mobx_node_express_sequelize\srcWeb\pages\Render3DGoods\render3DGoodsRenderer.ts
 * @Description: 3D商品渲染器，提供服务端渲染功能，支持puppeteer调用
 */



import { AmbientLight, DirectionalLight, PointLight, Vector3, BoxGeometry, Mesh, MeshStandardMaterial } from "three";
import ThreeBase from "three-base";

// 扩展Window接口声明
declare global {
    interface Window {
        notifyPuppeteer?: (data: string) => void;
        renderCanvas?: (params: Record<string, any>) => void;
    }
}

/**
 * @class Render3DGoodsRenderer
 * @extends {ThreeBase}
 */
class Render3DGoodsRenderer extends ThreeBase {

    constructor() {
        super();
        // 将渲染器实例挂载到window对象上，供puppeteer访问
        if (typeof window !== 'undefined') {
            window.render3DGoodsRenderer = this;
            console.log('[3D渲染器] 已挂载到window对象');
        }
    }

    // 环境光
    ambientLight = (() => {
        const ambientLight = new AmbientLight("#fff", 3);
        this.scene.add(ambientLight);
        return ambientLight;
    })();

    //点光源
    pointLight = (() => {
        const pointLight = new PointLight("#fff", 1.5);
        pointLight.position.set(50, 40, 50);
        this.scene.add(pointLight);
        return pointLight;
    })();

    // 方向光
    directionalLight = (() => {
        const directionalLight = new DirectionalLight("#fff", 1.5);
        directionalLight.position.set(-10, 10, 5);
        directionalLight.target.position.set(0, 0, 0);
        directionalLight.castShadow = true;
        directionalLight.shadow.mapSize.set(1024, 1024);
        this.scene.add(directionalLight);
        return directionalLight;
    })();

    box = (() => {
        const geometry = new BoxGeometry(20, 20, 20);
        // 创建材质（使用MeshStandardMaterial以支持光照）
        const material = new MeshStandardMaterial({
            color: 0x00ff00,
            metalness: 0.3,
            roughness: 0.4
        });
        // 创建网格
        const cube = new Mesh(geometry, material);
        // 设置立方体位置
        cube.position.set(0, 0, 0);
        // 添加到场景
        this.scene.add(cube);
        return cube;
    })();

    init() {
        super.init();
        this.autoRender = false;
        // this.axesHelper.visible = false;
        // this.gridHelper.visible = false;
        // this.controls.enableZoom = false;
        // this.controls.enablePan = false;

        this.setControlType("OrbitControls");
        this.resetLookAt({
            center: new Vector3(0, 0, 0),
            position: new Vector3(0, 0, 50)
        });
        // this.getCamera().position.set(0, 0, 50);
        // this.getCamera().lookAt(0, 0, 0);

        // 监听通知方法，用于接收puppeteer调用
        window.renderCanvas = (params: Record<string, any>) => {
            this.render();
            window.notifyPuppeteer(this.getThumbBase64());
        }

    }
}

const render3DGoodsRenderer = new Render3DGoodsRenderer();

export default render3DGoodsRenderer;
